//
//  HEAbourtViewController.m
//  HealthFamily
//
//  Created by LOOK on 2021/1/2.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEAbourtViewController.h"
#import "HEPublicCell.h"

@interface HEAbourtViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSArray *protcalArr;

@end

@implementation HEAbourtViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"关于";
    [self.view addSubview:self.tableView];
     
    [self initCell];
    [self initHeadView];
    
    _protcalArr = @[
                   @"https://www.honglutec.com/agreement/service.html",
                   @"https://www.honglutec.com/agreement/yszc.html",
                   @"https://www.honglutec.com/agreement/member.html",
                   @"https://www.honglutec.com/agreement/auth.html",
                   ];
}

- (void)initHeadView {
    UIView *warp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 160)];
    
    UIImageView *logo = [UIImageView new];
    logo.image = IMAGE_NAMED(@"logo");
    [warp addSubview:logo];
    
    [logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).offset(30);
        make.centerX.equalTo(warp);
        make.width.height.offset(60);
    }];
    
    // app名称
    NSString *appName = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleDisplayName"];
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    UILabel *nameLb = [UILabel new];
    nameLb.text = [NSString stringWithFormat:@"%@ v%@", appName, appVersion];
    nameLb.font = DYFont(16);
    nameLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:nameLb];
    
    [nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logo.mas_bottom).offset(15);
        make.centerX.equalTo(warp);
        make.left.equalTo(warp).offset(15);
        make.right.equalTo(warp).offset(-15);
        make.height.offset(30);
    }];
    
    self.tableView.tableHeaderView = warp;
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];

    NSMutableArray *classArr = [NSMutableArray array];

    // 普通cell
    GDFiterCellModel *publicModel = [GDFiterCellModel new];
    publicModel.cellKlass = HEPublicCell.class;
    publicModel.cellType = GDFiterCellTypeSettingPublic;
  
    
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[publicModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];
    
    
    //
    NSMutableArray *row1 = [NSMutableArray array];
    GDFiterCellModel *addresModel = [GDFiterCellModel new];
    addresModel.cellKlass = HEPublicCell.class;
    addresModel.leftTitle = @"服务协议";
    [row1 addObject:addresModel];
  
    GDFiterCellModel *prop2 = [GDFiterCellModel new];
    prop2.cellKlass = HEPublicCell.class;
    prop2.leftTitle = @"隐私政策";
    [row1 addObject:prop2];
    
    GDFiterCellModel *prop3 = [GDFiterCellModel new];
    prop3.cellKlass = HEPublicCell.class;
    prop3.leftTitle = @"健康老龄会员权益";
    [row1 addObject:prop3];
    
    GDFiterCellModel *prop4 = [GDFiterCellModel new];
    prop4.cellKlass = HEPublicCell.class;
    prop4.leftTitle = @"实名认证服务协议";
    [row1 addObject:prop4];
 
    
    [_sections addObject:row1];

    [_tableView reloadData];
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    return sec.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];
    
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];
    return 55;

//    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
//               [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
//    }];
}

#pragma mark -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 15)];
    v.backgroundColor = CViewBgColor;
    return v;
}

#pragma mark 解决cell分割线左侧留空的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 下面这几行代码是用来设置cell的上下行线的位置
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    //按照作者最后的意思还要加上下面这一段，才能做到底部线控制位置，所以这里按stackflow上的做法添加上吧。
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSURL * file = [NSURL URLWithString: _protcalArr[indexPath.row]];
 
    RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
    webView.isShowCloseBtn = YES;
    webView.showPageTitle = YES;
    [self.navigationController pushViewController:webView animated:YES];
}

@end
